package com.linkhub.oms.common.util;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.linkhub.oms.common.constant.SymbolConstant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description: QtBigDecimalUtil
 * @Author: echo
 * @Date: 2024/05/27 19:58
 * @Version: 1.0
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QtBigDecimalUtil {

    public static final BigDecimal BD_100 = BigDecimal.valueOf(100);
    public static final BigDecimal BD_1000 = BigDecimal.valueOf(1000);
    public static final BigDecimal BD_10000 = BigDecimal.valueOf(10000);
    public static final BigDecimal BD_TEN_BILLION = BigDecimal.valueOf(100000000);

    public static boolean equals(BigDecimal num1, BigDecimal num2) {
        if (num1 == null && num2 == null) {
            return true;
        }
        if (num1 == null || num2 == null) {
            return false;
        }
        return num1.compareTo(num2) == 0;
    }

    public static BigDecimal valueOf(String num) {
        if (NumberUtil.isNumber(num)) {
            return new BigDecimal(num);
        }
        return null;
    }

    public static BigDecimal min(BigDecimal a, BigDecimal b) {
        if (a == null || b == null) {
            throw new IllegalArgumentException();
        }
        return a.compareTo(b) >= 0 ? b : a;
    }

    public static boolean ge(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) >= 0;
    }

    public static boolean gt(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) > 0;
    }

    public static boolean le(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) <= 0;
    }

    public static boolean lt(BigDecimal a, BigDecimal b) {
        return a.compareTo(b) < 0;
    }

    public static boolean isBigger(BigDecimal big, BigDecimal small) {
        return big.compareTo(small) > 0;
    }

    public static boolean isSmaller(BigDecimal big, BigDecimal small) {
        return big.compareTo(small) < 0;
    }

    public static BigDecimal scale2(BigDecimal value) {
        if (null == value) {
            return null;
        }
        return value.setScale(2, RoundingMode.HALF_UP);
    }

    public static BigDecimal scale2(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return new BigDecimal(value).setScale(2, RoundingMode.HALF_UP);
    }

    public static BigDecimal scale4(BigDecimal value) {
        if (null == value) {
            return null;
        }
        return value.setScale(4, RoundingMode.HALF_UP);
    }

    public static BigDecimal scale4(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return new BigDecimal(value).setScale(4, RoundingMode.HALF_UP);
    }

    public static BigDecimal scale(BigDecimal value, int scale) {
        if (null == value) {
            return null;
        }
        return value.setScale(scale, RoundingMode.HALF_UP);
    }

    /**
     * 输出为字符串，去除末尾无效的0
     *
     * @param num 十进制数字
     * @return 输出结果
     */
    public static String stripToString(BigDecimal num) {
        return Optional.ofNullable(num)
                .map(BigDecimal::stripTrailingZeros)
                .map(BigDecimal::toPlainString)
                .orElse(null);
    }

    public static String toString(BigDecimal num, String defaultValue) {
        return Optional.ofNullable(num)
                .map(BigDecimal::toPlainString)
                .orElse(defaultValue);
    }


    public static String toString(BigDecimal num) {
        return toString(num, StringUtils.EMPTY);
    }
    /**
     * 如果所有元素非空，则进行相乘；否则返回{@code null}
     *
     * @param bigDecimals 相乘的数的集合
     * @return 运算结果
     */
    @SuppressWarnings("all")
    public static BigDecimal mulIfNotNull(BigDecimal... bigDecimals) {
        if (bigDecimals == null || ObjectUtil.hasEmpty(bigDecimals)) {
            return null;
        }

        BigDecimal result = bigDecimals[0];
        for (int i = 1; i < bigDecimals.length; i++) {
            result = result.multiply(bigDecimals[i]);
        }

        return result;
    }

    public static BigDecimal stripTrailingZeros(BigDecimal num) {
        return Optional.ofNullable(num)
                .map(BigDecimal::stripTrailingZeros)
                .orElse(null);
    }

    /**
     * 构建字符串格式的数值
     *
     * @param value 数值
     * @return 构建结果
     */
    public static String getNumStr(BigDecimal value) {
        return getNumStr(value, SymbolConstant.VIEW_NULL_STRING);
    }

    /**
     * 构建字符串格式的数值
     *
     * @param value        数值
     * @param defaultValue 数值为空时的默认值
     * @return 构建结果
     */
    public static String getNumStr(BigDecimal value, String defaultValue) {
        return Optional.ofNullable(value)
                .map(BigDecimal::toPlainString)
                .orElse(defaultValue);
    }

    /**
     * 元转亿元
     *
     * @param value1
     * @param scale
     * @param roundingMode
     * @return
     */
    public static BigDecimal toMillion(BigDecimal value1, int scale, RoundingMode roundingMode) {
        if (value1 == null) {
            return null;
        }
        return value1.divide(BD_TEN_BILLION, scale, roundingMode);
    }


    /**
     * 校验金额
     */
    public static boolean checkAmountValid(BigDecimal amount){
        if (Objects.isNull(amount)) {
            return false;
        }
        return !lt(amount, new BigDecimal(0));
    }


    public static BigDecimal defaultIfNull(BigDecimal value, BigDecimal defaultValue) {
        if (Objects.isNull(value)) {
            return defaultValue;
        }
        return value;
    }

}
